<?php

namespace App\Console\Commands;

use App\Common\Models\Db1\AliApp;
use App\Common\Models\Db1\MpApp;
use EasyWeChat\Factory;
use Illuminate\Console\Command;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

class WxCreateMenu extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'WxCreateMenu';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '创建公众号菜单';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $item_app = (new MpApp())->findOne(['id' => 1]);
        $config = [
            'app_id' => $item_app->app_id,
            'secret' => $item_app->app_secret,

            'token' => $item_app->token,
            'aes_key' => $item_app->encoding_aes_key,

            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',

            //...
        ];

        try {
            $app = Factory::officialAccount($config); // 公众号
            /*
               $work = EasyWeChat::work(); // 企业微信
               $payment = EasyWeChat::payment(); // 微信支付
               $openPlatform = EasyWeChat::openPlatform(); // 开放平台
               $miniProgram = EasyWeChat::miniProgram(); // 小程序
             */

            $app->menu->create([
                [
                    "name" => "功能",
                    "sub_button" => [
                        [
                            "type" => "view",
                            "name" => "支付测试",
                            "url" => "https://mp.test1test1.cn/good/list?app_id=" . $config['app_id']
                        ]
                    ]
                ],
                [
                    "name" => "优惠",
                    "sub_button" => [
                        [
                            "type" => "view",
                            "name" => "淘宝优惠",
                            "url" => "http://tb.zzzpianyi.cn/"
                        ]
                    ]
                ],
                [
                    "name" => "按钮3",
                    "sub_button" => [
                        [
                            "type" => "click",
                            "name" => "测试",
                            "key" => "测试"
                        ]

                    ]
                ]
            ]);
        } catch (\Exception $e) {
            echo $e->getMessage() . "\n";
        }
    }
}
